package com.imooc.business.processor;

import com.imooc.business.builder.RequestBuilder;
import com.imooc.business.core.DataProcess;
import com.imooc.module.collect.enums.DimensionEnum;
import com.imooc.module.collect.enums.PeriodEnum;
import com.imooc.module.collect.enums.RuleTypeEnum;
import com.imooc.module.collect.request.ParamsRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @Author Tim
 * @Description:
 * @Version 1.0
 */
@Slf4j
@Component
public class UpDimensionRequestProcessor {

    private final DataProcess dataProcess;

    public UpDimensionRequestProcessor(DataProcess dataProcess) {
        this.dataProcess = dataProcess;
    }

    /**
     * 空间维度：用户采集(基础维度) 完成 触发 群组 采集，群组采集完毕 触发 学校维度采集
     * 时间维度：日 -> 周 -> 月 -> 学期 -> 学年
     * @param request
     */
    public void callback(ParamsRequest request) {
        PeriodEnum period = PeriodEnum.parse(request.getPeriodType());
        DimensionEnum dimension = DimensionEnum.parse(request.getDimensionParameter().getDimension());
        if (period.equals(PeriodEnum.SCHOOL_YEAR) && dimension.equals(DimensionEnum.SCHOLL)) {
            log.info("当前一次采集完成， 业务id:{}, 采集日期:{}", request.getBusinessId(), request.getRequestDate());
            return;
        }
        //进行维度升级
        ParamsRequest paramsRequest = RequestBuilder.buildNextParamsRequest(request);
        if (Objects.nonNull(paramsRequest)) {
            PeriodEnum currentPeriod = PeriodEnum.parse(paramsRequest.getPeriodType());
            DimensionEnum currentDimension = DimensionEnum.parse(paramsRequest.getDimensionParameter().getDimension());
            log.info("维度升级计算开始，上一次时间维度:{},上一次的空间维度:{}, 当前计算时间维度:{}, 当前空间计算维度:{}",
                    period.getDescription(), dimension.getDescription(),
                    currentPeriod.getDescription(), currentDimension.getDescription());
            paramsRequest.setRuleTypeEnum(RuleTypeEnum.COLLECT);
            dataProcess.process(paramsRequest);
        }
    }
}
